package com.ag.work.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.ag.bean.TbDisposalAdvice;
import com.ag.dao.TbDisposalAdviceDAO;
import com.sun.org.apache.commons.logging.Log;
import com.sun.org.apache.commons.logging.LogFactory;

public class MyAdviceDAO extends TbDisposalAdviceDAO {

	private static final Log log = LogFactory.getLog(MyAdviceDAO.class);
	
	public List<TbDisposalAdvice> findMyAdvice(String uId) {
		
		/*String hql="from TbDisposalAdvice t join fetch t.tbUserByAdviceUId  " +
				"join fetch t.tbUserBySubmitUId join fetch t.tbUserByApplyUId " +
				"join fetch t.tbEvent.EId " +
				"where t.tbUserByAdviceUId.UId="+uId+" or t.tbUserBySubmitUId.UId="+uId+ "" +
						" or t.tbUserByApplyUId.UId= "+uId;*/
		String hql="from TbDisposalAdvice t left join fetch t.tbUserByAdviceUId  " +
		" left join fetch t.tbUserBySubmitUId left join fetch t.tbUserByApplyUId" +
		" left join fetch t.tbEvent where t.tbUserByAdviceUId.UId=:uid or t.tbUserBySubmitUId.UId =:uid " +
		" or t.tbUserByApplyUId.UId=:uid order by t.daApplyTime desc";
		Session session=null;
		Transaction transaction=null;
		List<TbDisposalAdvice> list = null;
		try {
			 session = (Session)this.getSessionFactory().openSession();
			transaction = session.beginTransaction();
			Query query = (Query) session.createQuery(hql).setString("uid", uId);
			list = query.list();
			session.getTransaction().commit();
		} catch (HibernateException e) {
			e.printStackTrace();
		} finally {
			if (transaction != null) {
				transaction.rollback();
			}
			if (session != null) {
				session.close();
			}
		}
		return list;
		
	}
	
	public List findRequest(String UId){
		log.info("打印出：UId----" + UId);
		log.debug("getting TbDisposalAdvice by id: " + UId);
		try {

			//String hql = "select distinct da.daId,da.tbEvent.EEventname,da.b,da.daContent,da.tbUserByAdviceUId.UName," +
			//		"da.daApplyTime,da.daLastestTime,da.daAcceptTime,da.daSubmitTime," +
			//		"da.c from TbDisposalAdvice da right join fetch da.tbUserByApplyUId right join fetch da.tbUserByAdviceUId right join fetch da.tbEvent where da.tbUserByApplyUId.UId = ?";
			//String hql = "select count(distinct da.daId) from TbDisposalAdvice da where da.tbUserByApplyUId.UId = ?";
			String hql = "from TbDisposalAdvice da left join fetch da.tbUserByApplyUId left join fetch da.tbUserByAdviceUId left join fetch da.tbEvent where da.tbUserByApplyUId.UId = ? order by da.daApplyTime desc";
			List list = getHibernateTemplate().find(hql, UId);
			log.info("输出我的申请列表：-------" + list);
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public List findMyAdvice1(String UId){
		log.info("打印出：UId----" + UId);
		log.debug("getting TbDisposalAdvice by id: " + UId);
		try {
			String hql = "from TbDisposalAdvice da left join fetch da.tbUserByAdviceUId left join fetch da.tbUserByApplyUId left join fetch da.tbEvent where da.tbUserByAdviceUId.UId = ? order by da.daApplyTime desc";
			List list = getHibernateTemplate().find(hql, UId);
			log.info("输出我的建议列表：-------" + list);
			return list;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	
	public TbDisposalAdvice findById(String daId){
		log.info("开始执行：findById" +daId);
		log.debug("getting TbDisposalAdvice by id: " + daId);
		try {
			String hql = "from TbDisposalAdvice da join fetch da.tbUserByAdviceUId join fetch da.tbUserByApplyUId join fetch" +
					" da.tbEvent where da.daId = "+ daId +" order by da.daApplyTime desc";
			TbDisposalAdvice instance = (TbDisposalAdvice) getHibernateTemplate().find(hql).get(0);
			log.info("instance:---"+instance);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

}
